'use strict'


const express = require('express')
const bodyParser = require('body-parser')
const fs = require('fs')
const multer = require('multer');
const path = require('path');


let app = express()





// 注册 body-parser 中间件
app.use(bodyParser({ extended: false }))
// app.use(bodyParser.urlencoded({ extended: false }));

//1.0 初始化orm
const orm = require('orm')

// 设置存储配置
const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    const date = new Date();
    const yearMonth = date.getFullYear().toString() + ('0' + (date.getMonth() + 1)).slice(-2);
    const day = ('0' + date.getDate()).slice(-2);
    const directory = path.join(__dirname, 'upload', `${yearMonth}`, day);
 
    // 确保目录存在
    if (!fs.existsSync(directory)){
        fs.mkdirSync(directory, { recursive: true });
    }
 
    cb(null, directory);
  },
  filename: function (req, file, cb) {
    cb(null, Date.now() + path.extname(file.originalname)); // 添加时间戳以避免文件名冲突
  }
});


app.use('/static', express.static(path.join(__dirname, 'upload')));
 
// 初始化multer
const upload = multer({ storage: storage });


// 定义文件上传接口
app.post('/api/uploadImage', upload.single('image'), (req, res) => {
  try {
    res.status(200).json({
      message: 'File uploaded successfully',
      file: req.file
    });
  } catch (error) {
    res.status(500).json({
      message: 'Error uploading file',
      error: error.message
    });
  }
});
 
 
app.use(
  orm.express('mysql://root:123456@127.0.0.1:3306/dtcmsdb4'
, {
    define: function(db, models, next) {
      next()
    }
  })
)

//2.0 将所有api的请求响应content-type设置为application/json
app.all('/api/*', (req, res, next) => {
  //设置允许跨域响应报文头
  //设置跨域
  //res.header("Access-Control-Allow-Origin", "*");
  //res.header("Access-Control-Allow-Headers", "X-Requested-With"); // X-Requested-With
  //res.header("Access-Control-Allow-Methods", "*");

  res.header('Access-Control-Allow-Origin', '*')
  // 设置服务器支持的所有头信息字段
  res.header(
    'Access-Control-Allow-Headers',
    'Content-Type,Content-Length, Authorization, Accept,X-Requested-With'
  )
  // 设置服务器支持的所有跨域请求的方法
  res.header('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS')

  res.setHeader('Content-Type', 'application/json;charset=utf-8')
  next()
})

//2.0 设置路由规则
const apiRoute = require('./routes/apiRoute.js')
app.use('/', apiRoute)
app.use(express.static(path.join(__dirname, 'public')))

app.listen(8082, () => {
   console.log('vue-cms的api服务已启动, :8082');
});
